package game.memc.base.shop;

import game.database.common.CommonDB;
import game.memc.utils.MemcacheInit;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import MemCache.MemCacheMethod;

import com.cs.city.bean.base.shop.ChargedService;

/**
 * 
 * @author Linzy
 * Create Date: 2010 3 22 11:50:21
 */
public class ChargedServiceDB {

	private static final Logger logger = Logger.getLogger(GoodsDB.class);

	private static MemCacheMethod mem = MemcacheInit.initMemCache();
	
	private static String keyType = "Goods_Type_";
	private static String keyId = "Goods_Bean_Id_";
	private static String keyLstType = "Goods_Lst_Type_";
	
	private static final String sqlAll = "SELECT id, desp, price, buyCount, " +
							"serStatus, createDate, updateDate, serviceType, " +
							"effectVal, workTime, blessStyle, blessName " +
							"FROM ChargedService ";

	private static ChargedService loadBean(ResultSet rs) {
		ChargedService bean = new ChargedService();
		try {
			bean.setId(rs.getLong("id"));
			bean.setDesp(rs.getString("desp"));
			bean.setPrice(rs.getInt("price"));
			bean.setSerStatus(rs.getInt("serStatus"));
			bean.setBuyCount(rs.getInt("buyCount"));
			bean.setServiceType(rs.getString("serviceType"));
			bean.setEffectVal(rs.getInt("effectVal"));
			bean.setWorkTime(rs.getLong("workTime"));
			bean.setBlessStyle(rs.getInt("blessStyle"));
			bean.setBlessName(rs.getString("blessName"));
			
			bean.setCreateDate(rs.getString("createDate"));
			bean.setUpdateDate(rs.getString("updateDate"));
		} catch (Exception e) {
			logger.info("	> ERR in ChargedServiceDB.getBeanLstDB() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		}
		return bean;
	} // loadBean()


	/**
	 * Get a bean by service type;
	 * 
	 * @param goodsId
	 * @return
	 * @deprecated use the other method which with the same name instead.
	 */
	public static ChargedService getBean(String serType) {
		String k = keyType + serType;
		try {
			Object obj = mem.get(k);
			if(obj != null){
				return (ChargedService)obj;
			}
			ChargedService bean = getBeanDB(serType);
			if(bean != null){
				mem.set(k, bean);
				return bean;
			}
		} catch (Exception e) {
			logger.info("	> ERR in ChargedServiceDB.getBean() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		}
		return null;
	} // getBean()

	/**
	 * Get a bean by service type from database;
	 * 
	 * @param serType
	 * @return
	 * @deprecated
	 */
	private static ChargedService getBeanDB(String serType) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		ChargedService bean = null;
		StringBuffer sql = new StringBuffer();
		try {
			sql.append(sqlAll);
			sql.append(" where serviceType=? ");
			sql.append(" and serStatus=1 ");

			conn = CommonDB.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, serType);
			
			rs = ps.executeQuery();
			if(rs.next()) {
				bean = loadBean(rs);
			}
		} catch (Exception e) {
			logger.info("	> ERR in ChargedServiceDB.getBeanDB() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		} finally {
			CommonDB.destroyConn(conn, null, ps, rs);
			rs = null;
			ps = null;
		}
		return bean;
	} // getBeanDB()



	/**
	 * Update a service buy count.
	 * 
	 * @param serviceId
	 * @param quantity
	 */
	public static boolean updateBuyCount(long serviceId, int quantity) {
		Connection conn = null;
		PreparedStatement ps = null;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE chargedService set buyCount=buyCount+" + quantity + 
					", updateDate=now() ");
		sql.append(" where id=?");
		try {
			conn = CommonDB.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setLong(1, serviceId);
			return ps.executeUpdate() > 0;
		} catch (Exception e) {
			logger.info("	> ERR in ChargedServiceDB.updateBuyCount() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
			return false;
		} finally {
			CommonDB.destroyConn(conn, null, ps, null);
			ps = null;
			conn = null;
		}
	} // updateBuyCount()


	/**
	 * Get a bean by service Id.
	 * @param serviceId
	 * @return
	 */
	public static ChargedService getBean(long serviceId) {
		String k = keyId + serviceId;
		try {
			Object obj = mem.get(k);
			if(obj != null){
				return (ChargedService)obj;
			}
			ChargedService bean = getBeanDB(serviceId);
			if(bean != null){
				mem.set(k, bean);
				return bean;
			}
		} catch (Exception e) {
			logger.info("	> ERR in ChargedServiceDB.getBean() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		}
		return null;
	} // getBean()

	/**
	 * Get a bean by serviceId from database.
	 * 
	 * @param serviceId
	 * @return
	 */
	private static ChargedService getBeanDB(long serviceId) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		ChargedService bean = null;
		StringBuffer sql = new StringBuffer();
		try {
			sql.append(sqlAll);
			sql.append(" where id=? ");
			sql.append(" and serStatus=1 ");

			conn = CommonDB.getConnection();
			ps = conn.prepareStatement(sql.toString());
			ps.setLong(1, serviceId);
			
			rs = ps.executeQuery();
			if(rs.next()) {
				bean = loadBean(rs);
			}
		} catch (Exception e) {
			logger.info("	> ERR in ChargedServiceDB.getBeanDB() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		} finally {
			CommonDB.destroyConn(conn, null, ps, rs);
			rs = null;
			ps = null;
		}
		return bean;
	} // getBeanDB()


	/**
	 * Get a list of leaf node service object by their parents' service type.
	 * 
	 * @param parSerType String; parent's service type.
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<ChargedService> getBeanLst(String parSerType) {
		String k = keyLstType + parSerType;
		try {
			Object obj = mem.get(k);
			if(obj != null){
				return (List<ChargedService>)obj;
			}
			List<ChargedService> beanLst = getBeanLstDB(parSerType);
			if(beanLst != null && beanLst.size() > 0){
				mem.set(k, beanLst);
				return beanLst;
			}
		} catch (Exception e) {
			logger.info("	> ERR in ChargedServiceDB.getBeanLst() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		}
		return null;
	} // getBeanLst()

	/**
	 * Get a list of bean from database.
	 * 
	 * @param parSerType
	 * @return
	 */
	private static List<ChargedService> getBeanLstDB(String parSerType) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		ChargedService bean = null;
		List<ChargedService> beanLst = new ArrayList<ChargedService>();
		
		StringBuffer sql = new StringBuffer();
		try {
			sql.append(sqlAll);
			sql.append(" where serviceType like '%" + parSerType + "%' ");
			sql.append(" and serStatus=1 order by createDate");
			
			conn = CommonDB.getConnection();
			ps = conn.prepareStatement(sql.toString());
//			ps.setString(1, parSerType);
			
			rs = ps.executeQuery();
			while(rs.next()) {
				bean = loadBean(rs);
				if(bean != null){
					beanLst.add(bean);
				}
			}
		} catch (Exception e) {
			logger.info("	> ERR in ChargedServiceDB.getBeanLstDB() - " + e.toString());
			logger.error(e.toString());
			e.printStackTrace();
		} finally {
			CommonDB.destroyConn(conn, null, ps, rs);
			rs = null;
			ps = null;
		}
		return beanLst;
	} // getBeanLstDB()


	
	
} // end of class